﻿#include "frmdrawer.h"

#include <QPainter>

frmDrawer::frmDrawer(QWidget *parent) :
    QWidget(parent)
    ,m_draw_type(DrawNone)
{
    QPalette pa(this->palette());
    pa.setColor(QPalette::Background, QColor(Qt::black));
    this->setPalette(pa);
    this->setAutoFillBackground(true);
}

frmDrawer::~frmDrawer()
{

}

void frmDrawer::paintEvent(QPaintEvent *  /*event*/)
{
    switch (m_draw_type) {
    case DrawLine:
        draw_line();
        break;
    case DrawArc:
        draw_arc();
        break;
    case DrawLayer:
        draw_layer();
        break;
    default:
        break;
    }
}

void frmDrawer::draw(frmDrawer::DrawType type)
{
    m_draw_type = type;
    update();
}

void frmDrawer::draw_line()
{
    QPainter painter(this);
    QPen pen(painter.pen());
    QRect rt(rect());
    pen.setColor(Qt::red);

    for (int i = 0; i != 10; ++i)
    {
        pen.setWidth(i);
        painter.setPen(pen);
        painter.drawLine(QLine(rt.left() + 20, 20 + i * 30, rt.right() - 20, 20 + i * 30));
    }
}

/*
 * 圆弧并不是一个真正的圆弧, 而是给出一个指定的矩形, 在矩形范围内画出圆弧
 * 圆弧的角度最小单位为 1/16 度
 * 角度为正数表示 ccw, 负数为 cw
 * startAngle 起始角度  spanAngle为增量角度
*/
void frmDrawer::draw_arc()
{
    QPainter painter(this);
    QPen pen(painter.pen());
    QRect rt(rect());
    pen.setColor(Qt::blue);

    pen.setWidth(0);
    painter.setPen(pen);
    painter.drawArc(rt, 0, 16 * 360);

    rt.setLeft(rt.left() + 50);
    rt.setRight(rt.right() - 50);
    rt.setTop(rt.top() + 50);
    rt.setBottom(rt.bottom() - 50);
    pen.setColor(Qt::red);
    painter.setPen(pen);
    painter.drawArc(rt, 0, 16 * 180);
    pen.setColor(Qt::green);
    painter.setPen(pen);
    painter.drawArc(rt, 16 * 180, 16 * 90);
}

void frmDrawer::draw_layer()
{
    QPainter painter(this);
    painter.setCompositionMode(QPainter::RasterOp_SourceOrDestination);

    QRect rt(rect());

    QBrush br(Qt::red);
    painter.fillRect(rt.left()
                     , rt.top()
                     , rt.width() * 2 / 3
                     , rt.height() * 2 / 3
                     , br);

    //br.setStyle(Qt::Dense3Pattern);
    br.setColor(Qt::blue);

    // 测试清除负性图案
    if (0)
    {
        const QPalette &pa = this->palette();
        br.setColor(pa.color(QPalette::Background));
        painter.setCompositionMode(QPainter::RasterOp_SourceAndDestination);
    }


    const QPalette &pa = this->palette();
    QPixmap pm(rt.width(), rt.height());
    pm.fill(pa.color(QPalette::Background));

    {
        QPainter pp(&pm);

        pp.fillRect(rt.left() + rt.width() / 2
                         , rt.top() + rt.height() / 2
                         , rt.right()
                         , rt.bottom()
                         , br);

        // 负性处理
        br.setStyle(Qt::Dense3Pattern);
        if (1)
        {
            br.setColor(pa.color(QPalette::Background));
            pp.setCompositionMode(QPainter::RasterOp_SourceAndDestination);
            pp.fillRect(rt.left() + rt.width() / 3
                             , rt.top() + rt.height() / 3
                             , rt.width() / 4
                             , rt.height() / 2
                             , br);

            //显示负性
            if (1)
            {
                br.setColor(Qt::blue);
                pp.setCompositionMode(QPainter::RasterOp_SourceOrDestination);
                pp.fillRect(rt.left() + rt.width() / 3
                                 , rt.top() + rt.height() / 3
                                 , rt.width() / 4
                                 , rt.height() / 2
                                 , br);
            }
        }

    }

    painter.drawPixmap(rt, pm);
}






